package com.example.zwh.computershop.shopping_cart;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.zwh.computershop.Myapp;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.sqlModel.Cart;
import com.example.zwh.computershop.sqlModel.Order;
import com.example.zwh.computershop.sqlModel.Product;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class shopcar_order extends AppCompatActivity {

    private final static String TAG = "shopcar_order";

    private static List<myOrder> OrderList = new ArrayList<>();
    private static ArrayList<Order> orders = new ArrayList<>();
    private static Myapp myapp;
    private static double allMoney;
    private static RecyclerView recyclerView;
    private ShopOrderAdapter shopOrderAdapter;

    private Button btnTitle;
    private Button btnBuy;

    private TextView tvUserName;
    private TextView tvUserPhone;
    private TextView tvUserDir;
    private TextView tvShopNum;
    private TextView tvShopPrice;

    private void init() {
        OrderList.clear();
        Bundle bundle = getIntent().getExtras();
        myapp = (Myapp) this.getApplication();
        //得到对应的购物车商品
        for (int j = 0; j < bundle.size(); j++) {
            Log.d(TAG, "onCreate: wxm" + bundle.getInt(String.valueOf(j)));
            for (Cart c : myapp.getCart()) {
                if (c.getProductId().equals(bundle.getString(String.valueOf(j))) && c.getId() == myapp.getUser().getId()) {
                    Log.d(TAG, "onCreate: " + c.getId());
                    for (Product p : myapp.getProducts()) {
                        if (c.getProductId().equals(p.getProductId())) {
                            OrderList.add(new myOrder(myapp.getUser().getId(), p.getProductId(), p.getImgUrl(), p.getProductName(), p.getBrand(), p.getPrice(), c.getAddNum()));
                        }
                    }
                } else continue;
            }
        }
    }

    private static double getMoney() {
        allMoney = 0.0;
        for (myOrder i : OrderList) {
            allMoney += i.getPrice() * i.getNum();
        }
        return allMoney;
    }

    private void delete() {
        Iterator<myOrder> order = OrderList.iterator();
        Iterator<Cart> cart = myapp.getCart().iterator();
        while (order.hasNext()) {
            myOrder o = order.next();
            while (cart.hasNext()) {
                Cart c = cart.next();
                if (c.getProductId().equals(o.getProductId())  && myapp.getUser().getId() == c.getId()) {
                    Log.d(TAG, "delete: 删除");
                    SQLoperation.deleteCart(c);
                    SQLoperation.setOrder(o);
                    //更新本地信息
                    //加入本地订单
                    Order order1 = new Order();
                    order1.setId(myapp.getUser().getId());
                    order1.setProductId(c.getProductId());
                    order1.setProductNum(c.getAddNum());
                    orders.add(order1);
                    //删除本地购物车
                    cart.remove();
                    break;
                }
            }
        }
        myapp.setOra(orders);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopcar_order);
        init();
        tvUserName = findViewById(R.id.tv_recipients);
        tvUserPhone = findViewById(R.id.tv_shop_car_phone);
        tvUserDir = findViewById(R.id.tv_direction);
        tvShopNum = findViewById(R.id.tv_shop_order_num);
        tvShopPrice = findViewById(R.id.tv_shop_order_money);

        tvUserName.setText("收件人" + myapp.getUser().getUsername());
        tvUserPhone.setText("联系方式" + myapp.getUser().getPhone());
        tvUserDir.setText("收件地址" + myapp.getUser().getHeadPortraitUrl());
        tvShopNum.setText("共有" + OrderList.size() +"件宝贝");
        tvShopPrice.setText("￥" + getMoney());

        btnTitle = findViewById(R.id.btn_shop_order_title);
        btnTitle.setOnClickListener(listener);
        btnBuy = findViewById(R.id.btn_shop_order_buy);
        btnBuy.setOnClickListener(listener);
        recyclerView = findViewById(R.id.rlv_shop_order);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        shopOrderAdapter = new ShopOrderAdapter(this, OrderList);
        recyclerView.setAdapter(shopOrderAdapter);
    }

    private View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_shop_order_title:
                    finish();
                    Log.d(TAG, "onClick: 取消订单");
                    break;
                case R.id.btn_shop_order_buy:
                    //生成订单后删除购物车对应商品

                    delete();
                    finish();
                    Log.d(TAG, "onClick: 订单生成");
                    break;
            }
        }
    };

}


